/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Portions created by the Initial Developer are
 * Copyright (C) 2012 the Initial Developer.
 * All Rights Reserved.
 *
 * The Original Code is "http://semi-frame.googlecode.com/svn/trenk/".
 *
 * The Initial Developer of the Original Code is the author below.
 */
/**
 * @file TcpTaskBase.cpp
 * @brief auto generated.
 * @author morningcloud
 * @date 2011/03/11
 */

#include "semi/TcpTaskBase.h"

namespace semi {

TcpTaskBase::TcpTaskBase() {

}

TcpTaskBase::~TcpTaskBase() {
	wait(0);
}

Param TcpTaskBase::preProcess(Param params) {

	int pid = 0;
	if (0 == (pid = fork())) {
		conf::NetConfig::TcpConfig cnf = this->config.getTcpConfig(
				this->getTaskId());
		this->net.start(cnf);
		exit(0);
	}

	if (this->getLogger().isDebugEnabled())
		this->getLogger().debug("preProcess(tcp task) of " + this->getTaskId()
				+ " start tcp receiver");

	NetParam prm = this->toNetParam(params);
	return this->toParam(this->preProcess(prm));
}

Param TcpTaskBase::endProcess(Param params) {

	if (this->getLogger().isDebugEnabled())
		this->getLogger().debug("endProcess(tcp task) of " + this->getTaskId()
				+ " start.");

	NetParam prm = this->toNetParam(params);
	return this->toParam(this->endProcess(prm));
}

Param TcpTaskBase::process(Param params) {

	if (this->getLogger().isDebugEnabled())
		this->getLogger().debug("process(tcp task) of " + this->getTaskId()
				+ " start.");

	NetParam prm = this->toNetParam(params);
	return this->toParam(this->process(prm));
}

NetParam TcpTaskBase::preProcess(NetParam params) {
	if (this->getLogger().isDebugEnabled())
		this->getLogger().debug("preProcess(tcp task) of " + this->getTaskId()
				+ " is empty.");
	return params;
}

NetParam TcpTaskBase::endProcess(NetParam params) {
	if (this->getLogger().isDebugEnabled())
		this->getLogger().debug("endProcess(tcp task) of " + this->getTaskId()
				+ " is empty.");
	return params;
}

NetParam TcpTaskBase::process(NetParam params) {
	if (this->getLogger().isDebugEnabled())
		this->getLogger().debug("process(tcp task) of " + this->getTaskId()
				+ " is empty.");
	return params;
}

NetParam TcpTaskBase::toNetParam(Param params) {
	NetParam prm;
	prm.success = params.success;
	prm.message = malloc(params.size);
	memcpy(prm.message, params.message, params.size);
	if (0 < params.message)
		free(params.message);
	prm.size = params.size;
	return prm;
}

Param TcpTaskBase::toParam(NetParam params) {
	Param prm;
	prm.success = params.success;
	prm.message = (char*) malloc(params.size);
	memcpy(prm.message, params.message, params.size);
	if (0 < params.message)
		free(params.message);
	prm.size = params.size;
	return prm;
}
}
